package com.wzr.cusmall.util;

import com.wzr.cusmall.dao.impl.CommodityDao;
import com.wzr.cusmall.global.Const;

import java.io.File;
import java.util.regex.Pattern;

public class CommonTools {

    /**
     * 读取数据库配置信息, 新建一个CommodityDao
     *
     * @param appPath 应用的实际路径
     * @return CommodityDao 对象
     */
    public static CommodityDao getCommodityDao(String appPath) {
        String dbPropertiesPath = appPath + File.separator
                + Const.PROPERTIES_DBCONFIG;
        DbConfig dbConfig = new DbConfig(dbPropertiesPath);
        String url = dbConfig.getUrl();
        String dbuser = dbConfig.getUserName();
        String dbpassword = dbConfig.getPassword();
        String dbDriver = dbConfig.getDriver();
        return new CommodityDao(url, dbDriver, dbuser, dbpassword);
    }


    /**
     * 判断是否为正整数
     *
     * @param str
     * @return
     */
    public static boolean isPositiveInt(String str) {
        if (null == str) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

/*
    public static Map<String, String> getMapFromParts(Collection<Part> parts) {
        if (null == parts) {
            return null;
        }
        Map<String,String> map = new HashMap<>();
            for(Part part : parts){
                if(null == part.getContentType()){
                    // 非二进制文件, 生成Map
                    map.put(part.getName(), part.)

                }
            }

    }*/
}
